import {Component, OnInit, Input} from '@angular/core';
import {ApiService} from "@core/services/http/api.service";
import {GridConfigService} from 'app/core/config/grid-config.service';
import {GridOptions} from '@ag-grid-community/core';
import {ActivatedRoute}  from "@angular/router";
import {ApesMessageService} from "@shared/apes/message";
import {UserAuthService} from "@core/services/user-auth.service";
import { BiService } from '../../bi.service';

@Component({
  selector: 'apes-case-administration',
  templateUrl: './case-administration.component.html',
  styles: []
})
export class CaseAdministrationComponent implements OnInit {
  buttonBar = [{hide: true, style: 'default', label: '查询', id: 'action_query',loading: false},
  ];
  title = '';
  ctx = {model: {}};
  organizationData : any[];
  rbCount:any[] = [];
  rsCount:any[] = [];
  qtCount:any[] = [];
  /** Grid 相关 **/
  resultData: any;
  gridOptions: GridOptions;
  pinnedBottomRowData: any[] = [];
  columnDefs: any[] = [];
  height: any = 400 + 'px';
  userInfo = this.userAuthService.getUserInfoObj();
  companyId = this.userInfo.userAccount.post.company.id;
  companyData: any;
  companyList: any[];

  constructor(private apiService: ApiService,
              private gridConfigService: GridConfigService,
              private routerInfo: ActivatedRoute,
              private msg: ApesMessageService,
              private userAuthService: UserAuthService,
              private bi: BiService) {
    this.columnDefs = [
      {headerName: '案件数量',
        children: [
          { headerName: "案件类型",field: 'AJLY' },
          { headerName: "数量" ,field: 'SL' }
        ]
      },
      {headerName: '报案',
        children: [
          { headerName: "单方事故" ,field: 'DFSG' },
          { headerName: "双方事故",field: 'SFSG'  },
          { headerName: "标的" ,field: 'BD' },
          { headerName: "第三者",field: 'DSZ'  },
          { headerName: "伤亡案" ,field: 'SWA' },
          { headerName: "物损案" ,field: 'WSA' },
          { headerName: "销案" ,field: 'XA' }
        ]
      },
      {headerName: '到店',
        children: [
          { headerName: "今日到店" ,field: 'JRDD' },
          { headerName: "标的到店" ,field: 'BDDD' },
          { headerName: "第三者到店" ,field: 'DSZDD' },
          { headerName: "拖车到店" ,field: 'TCDD' }
        ]
      },
      {headerName: '跟踪',
        children: [
          { headerName: "今日待跟踪" ,field: 'JRDGZ' },
          { headerName: "商务待跟踪" ,field: 'SWDGZS' },
          { headerName: "门店待跟踪" ,field: 'MDDGZS' }
        ]
      },
      {headerName: '流失',
        children: [
          { headerName: "今日流失",field: 'JRLS'  }
        ]
      },
      {headerName: '无效案件',
        children: [
          { headerName: "无效案件",field: 'WXAJS'  }
        ]
      },
    ];

    // 初始化 grid
    this.gridOptions = <GridOptions>{
      toolPanelSuppressSideButtons: true,
      suppressExcelExport: true,
      suppressCsvExport: true,
      showToolPanel: false,
      pagination: true,
      paginationPageSize: 30,
      rowHeight: 35
    };
    this.gridOptions.localeText = gridConfigService.getLocaleText();
    this.companyList = [];
    this.companyList.push(this.companyId);
  }

  ngOnInit() {
    this.bi.getCompany(this.userInfo.userAccount.post.localPermit.id).then(value => { this.companyData = value });
    this.submit();
  }

  onEvent(data: any) {
    if (data instanceof MouseEvent) return;
    if (data) {
      if (data.id === 'action_query') {
        this.buttonBar[0].loading = true;
        this.submit();
      }
    }
  }

  submit() {
    // if(this.companyList.length <= 0){
    //   this.companyList.push(this.companyId);
    // }
    this.getTablesData();
    this.buttonBar[0].loading = false;
  }

  getTablesData() {
    let data = { 'interfaceMethod': 'ipmBusinessStatementService.getBagl', 'pdata': {'companyId': this.companyList} };
    this.apiService.post('report.interface', { 'body': JSON.stringify(data) }).subscribe(
      (resultData) => {
        const result = JSON.parse(resultData);
        if (result.success) {
          if (result.obj.length !== 0) {
            let data = JSON.parse(result.obj);
            let hearderTot = data['BAZJ'];
            for (let i = 0, len = hearderTot.length; i < len; i++) {
              let tempData = hearderTot[i];
              tempData.headerConten = data[tempData.id];
            }
            this.organizationData = hearderTot;
            this.rbCount = data['RB'];
            this.rsCount = data['RS'];
            this.qtCount = data['QTGS'];
          }
        } else {
          this.msg.info(result.msg);
        }
      });
  }

  headerConten = [
    { track: '车险报案', headerTotal: 'CXBAS'},
    { track: '单方事故',headerTotal: 'DFSG' },
    { track: '双方事故',headerTotal: 'SFSG' },
    { track: '标的' ,headerTotal: 'BD'},
    { track: '第三者',headerTotal: 'DSZ' },
    { track: '伤亡案' ,headerTotal: 'SWA'},
    { track: '物损案', headerTotal: 'WSA' },
    { track: '销案' , headerTotal: 'XA' },
    { track: '到店', headerTotal: 'JRDD'  },
    { track: '跟踪' , headerTotal: 'GZS' },
    { track: '流失' , headerTotal: 'LS' },
    { track: '无效案件' , headerTotal: 'WXAJ' },
  ];

}
